package com.hzcx.app.simplechat.ui.setting;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.ui.setting.adapter.VipSettingAdapter;
import com.hzcx.app.simplechat.ui.setting.bean.VipSettingBean;
import com.hzcx.app.simplechat.ui.setting.contract.VipSettingContract;
import com.hzcx.app.simplechat.ui.setting.presenter.VipSettingPresenter;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;

/**
 * 会员功能设置
 */
public class VipSettingActivity extends BaseActivity<VipSettingPresenter> implements VipSettingContract.View {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.rv_vip_setting)
    RecyclerView rvVipSetting;

    private List<VipSettingBean> listData;
    private VipSettingAdapter settingAdapter;
    private boolean isSwitchReady=true;

    @Override
    public int getLayout() {
        return R.layout.activity_vip_setting;
    }

    @Override
    public void initPresenter() {
        mPresenter = new VipSettingPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText("会员功能");

        listData = new ArrayList<>();
        settingAdapter = new VipSettingAdapter(listData);
        rvVipSetting.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        rvVipSetting.setAdapter(settingAdapter);
    }

    @Override
    public void initData() {

        mPresenter.getMineVipSetting(this);

        settingAdapter.setOnSwitchChangeListener(new VipSettingAdapter.OnSwitchChangeListener() {
            @Override
            public void change(int pos, boolean isChecked) {
                if(!isSwitchReady){
                    return;
                }
                mPresenter.updateVipSetting(VipSettingActivity.this, listData.get(pos).getList().getType(), pos);
                isSwitchReady=false;
            }
        });
    }

    /**
     * 会员设置获取成功
     *
     * @param data
     */
    @Override
    public void settingResult(List<VipSettingBean> data) {
        try {
            listData.clear();
            listData.addAll(data);
            settingAdapter.notifyDataSetChanged();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 会员设置修改成功
     *
     * @param pos
     */
    @Override
    public void updateSuccess(int pos) {
        try {
            listData.get(pos).getList().setSate(listData.get(pos).getList().getSate() == 0 ? 1 : 0);
            settingAdapter.notifyDataSetChanged();
            isSwitchReady=true;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 会员设置修改失败
     */
    @Override
    public void updateFail() {
        settingAdapter.notifyDataSetChanged();
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                isSwitchReady=true;
            }
        },1000);
    }
}
